class Map {
    
    FUEL_SPAWN_RATE = 0;
    cost = 0;
    
    start = null;
    end = null;

    cells = Array.from(Array(0), () => new Array(0));

    constructor (x, y, FUEL_SPAWN_RATE, cost) {
        
        this.cells = Array.from(Array(x), () => new Array(y));
        this.FUEL_SPAWN_RATE = FUEL_SPAWN_RATE;
        this.cost = cost;
    }
    
    // func which sets start
    setStart(i, j) {
        this.cells[i][j].isStart = true;
        //this.cells[i][j].isPath = true;
        this.start = this.cells[i][j];
    }

    //func which sets end
    setEnd(i, j) {
        this.cells[i][j].isEnd = true;
        this.end = this.cells[i][j];
    }

    // init map func
    initMap() {
        var x = this.cells.length;
        var y = this.cells[0].length;

        for (var i = 0; i < x; i++) {
            for (var j = 0; j < y; j++) {
                this.cells[i][j] = new Cell(i, j, 0, true);
            }
        }

        for (var i = 1; i < x - 1; i++) {
            for (var j = 1; j < y - 1; j++) {

                var f = Math.round(Math.random(0, 1) * 100);
                if (f < FUEL_SPAWN_RATE) {
                    f = 0;
                }
                // var fuel = Math.round(Math.random(0, 1) * 100);
                this.cells[i][j] = new Cell(i, j, f, false);
            }
        }
    }

    f(current, x) {
        //console.log("до вайла")
        while (x > 0) {
            if (current.isVisible) {
                //console.log("в вайле")
                if (current.edges[1] != null) {
                    this.f(current.edges[1].target, x - 1);
                    console.log("x=[" + x + "], target.i=[" + current.edges[1].target.i + "], target.j=[" + current.edges[1].target.j + "]");
                }
                if (current.edges[3] != null) {
                    this.f(current.edges[3].target, x - 1);
                    console.log("x=[" + x + "], target.i=[" + current.edges[3].target.i + "], target.j=[" + current.edges[3].target.j + "]");
                }
                if (current.edges[4] != null) {
                    this.f(current.edges[4].target, x - 1);
                    console.log("x=[" + x + "], target.i=[" + current.edges[4].target.i + "], target.j=[" + current.edges[4].target.j + "]");
                }
                if (current.edges[6] != null) {
                    this.f(current.edges[6].target, x - 1);
                    console.log("x=[" + x + "], target.i=[" + current.edges[6].target.i + "], target.j=[" + current.edges[6].target.j + "]");
                }

                current.edges[1] = null;
                current.edges[3] = null;
                current.edges[4] = null;
                current.edges[6] = null;
            }
            x = x - 1;
        }
    }
    deleteAllEdges(current) {
        if (current.edges[1] != null) {            
            current.edges[1].target.edges[6] = null;
            current.edges[1] = null;
        }
        if (current.edges[3] != null) {
            current.edges[3].target.edges[4] = null;
            current.edges[3] = null; 
        }
        if (current.edges[4] != null) {
            current.edges[4].target.edges[3] = null;
            current.edges[4] = null; 
        }
        if (current.edges[6] != null) {
            current.edges[6].target.edges[1] = null;
            current.edges[6] = null; 
        }
    }

    // func which creates connections for all cells in map
    refreshGraph(size) {
        for (var i = 0; i < this.cells.length; i++) {
            for (var j = 0; j < this.cells[0].length; j++) {
                var current = this.cells[i][j];
                //var w = Math.round(Math.random(0, 1) * 50);
                var w = this.cost;
                //if (!current.isVisible) {
                    if((j - 1) >= 0) current.edges[1] = new Edge(w, this.cells[i][j-1]);
                    if((i - 1) >= 0) current.edges[3] = new Edge(w, this.cells[i-1][j]);
                    if((i + 1) < this.cells.length) current.edges[4] = new Edge(w, this.cells[i+1][j]);
                    if((j + 1) < this.cells[0].length) current.edges[6] = new Edge(w, this.cells[i][j+1]);
                //}
                // else if (c.isPath) {

                //     if (c.edges[1] != null) {
                //         c.edges[1].target.edges[6] = null;
                //     }
                //     c.edges[1] = null;
                       
                //     if (c.edges[3] != null) {
                //         c.edges[3].target.edges[4] = null;
                //     }                    
                //     c.edges[3] = null;
                    
                //     if (c.edges[4] != null) {
                //         c.edges[4].target.edges[3] = null;   
                //     }                                 
                //     c.edges[4] = null;
                    
                //     if (c.edges[6] != null) {
                //         c.edges[6].target.edges[1] = null;
                //     }                    
                //     c.edges[6] = null;
                // // }
                // else {

                //     if((j - 1) >= 0) current.edges[1] = !this.cells[i][j-1].isWall ? new Edge(w, this.cells[i][j-1]) : null;
                //     if((i - 1) >= 0) current.edges[3] = !this.cells[i-1][j].isWall ? new Edge(w, this.cells[i-1][j]) : null;
                //     if((i + 1) < this.cells.length) current.edges[4] = !this.cells[i+1][j].isWall ? new Edge(w, this.cells[i+1][j]) : null;
                //     if((j + 1) < this.cells[0].length) current.edges[6] = !this.cells[i][j+1].isWall ? new Edge(w, this.cells[i][j+1]) : null;
                // }
            }
        }
        for (var i = 0; i < this.cells.length; i++) {
            for (var j = 0; j < this.cells[0].length; j++) {
                var current = this.cells[i][j];

                
                if (current.isWall && current.isVisible) {
                    var border = (size == 1) ? 0 : ((size == 3) ? 1 : ((size == 5) ? 2 : ((size == 7) ? 3 : 0)));
                    var ii = i - border < 0 ? 0 : i - border;
                    var jj = j - border < 0 ? 0 : j - border;
                    var iii = i + border + 1 > this.cells.length ? this.cells.length : i + border + 1;
                    var jjj = j + border + 1 > this.cells[0].length ? this.cells[0].length : j + border + 1;
                    console.log("ii = " + ii + "; jj = " + jj + "; iii = " + iii + "; jjj = " + jjj + "; border = " + border)
    
                    for (var z = ii; z < iii; z++) {
                        for (var y = jj; y < jjj; y++) {
                            if (this.cells[z][y].isVisible && !this.cells[z][y].isEnd) {
                                console.log("cell")
                                console.log(this.cells[z][y])
                                this.deleteAllEdges(this.cells[z][y]);
                                // var border = (size == 1) ? 0 : ((size == 3) ? 1 : ((size == 5) ? 2 : ((size == 7) ? 3 : 0)));
                                // console.log(border)
                                // this.f(current, border);
                            }
                        }
                    }
                }   
            }
        }
    }
}